﻿Imports Microsoft.VisualBasic
Imports System.Data


Public Module Shared_Routines

    Const TRUNCATE_COUNT As Integer = 50

    Public Function truncate(ByVal originalInput As String) As String
        Return truncate(originalInput, TRUNCATE_COUNT)
    End Function

    Public Function truncate(ByVal originalInput As String, ByVal wordsLimit As Integer) As String
        If Not originalInput Is Nothing AndAlso originalInput <> "" AndAlso IsDBNull(originalInput) = False Then
            Dim output As New Text.StringBuilder(originalInput.Length)
            Dim input As New Text.StringBuilder(originalInput)
            Dim words As Integer = 0
            Dim lastwasWS As Boolean = True
            Dim count As Integer = 0
            Do
                If Char.IsWhiteSpace(input.Chars(count)) Then
                    lastwasWS = True
                Else
                    If lastwasWS Then words += 1
                    lastwasWS = False
                End If
                output.Append(input.Chars(count))
                count += 1
            Loop While (words < wordsLimit Or lastwasWS = False) And count < (originalInput.Length)

            Return output.ToString
        Else
            Return "-"
        End If
    End Function

    Public Function Encode(ByVal contents As Object) As String
        Return HttpUtility.HtmlEncode(CStr(contents))
    End Function

    Public Function encodeAndTruncate(ByVal contents As Object) As String
        Return truncate(Encode(contents))
    End Function

    Function GetFriendlyDate(ByVal src As Date, ByVal showtime As Boolean) As String
        Dim str As String
        Dim currdate As Date = Now
        Dim datediff As Integer = (src - currdate).Days
        Select Case datediff
            Case -1 : str = "Yesterday at "
            Case -7 To -2 : str = "Last " & src.DayOfWeek.ToString
            Case 0 : str = "Today "
            Case 1 To 7 : str = "This " & src.DayOfWeek.ToString
            Case Else : str = src.ToShortDateString()
        End Select
        If showtime = True Then
            str += " at " & src.ToShortTimeString()
        End If
        Return str
    End Function

    Function GetFriendlyDate(ByVal src As Date) As String
        Dim str As String
        Dim currdate As Date = Now
        Dim datediff As Integer = (src - currdate).Days
        Select Case datediff
            Case -1 : str = "Yesterday at "
            Case -7 To -2 : str = "Last " & src.DayOfWeek.ToString
            Case 0 : str = "Today "
            Case 1 To 7 : str = "This " & src.DayOfWeek.ToString
            Case Else : str = src.ToShortDateString()
        End Select
        Return str
    End Function

    Public Function NumPages(ByVal RowCount As Integer) As Integer
        Dim count As Integer = RowCount

        Dim pagesize As Integer = ClubStarterKit.Web.Settings.PageSize()

        Dim dbl As Double = count / 5
        Dim rounded As Double = Math.Ceiling(dbl)
        Dim testint As Integer = Integer.Parse(rounded)

        If testint > 0 Then
            Return testint
        Else
            If count > 0 Then
                Return 1
            Else
                Return 0
            End If
        End If
    End Function

End Module
